<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_member_for_mall;
use cmf\lib\Plugin;
use think\db;

class QbtMemberForMallPlugin extends Plugin
{

    public $info = [
        'name'        => 'QbtMemberForMall',
        'title'       => '会员管理',
        'description' => '可用于商城，需先按装【地区管理】插件',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com',
    ];


    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        //省市区表
        $result1 = Db::query("show tables like 'qbtcmf_region'");
        if (!count($result1)){
            return false;//需按装省市区插件
        }

        //会员表
        Db::query("DROP TABLE IF EXISTS `qbtcmf_mall_members`");
        Db::query("
            CREATE TABLE `qbtcmf_mall_members`  (
              `user_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `email` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '邮箱',
              `user_name` varchar(155) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '用户名',
              `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '密码',
              `sex` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '性别',
              `birthday` date NOT NULL DEFAULT '0000-00-00' COMMENT '出生年月',
              `user_money` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '可用金钱额度',
              `frozen_money` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '冻结金钱额度',
              `integral` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '积分',
              `last_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '最后登录时间',
              `last_ip` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '最后登录ip',
              `parent_id` mediumint(9) UNSIGNED NOT NULL DEFAULT 0 COMMENT '上级id',
              `mobile_phone` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '手机',
              `is_validated` tinyint(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否认证',
              `password_account_replay` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '提现密码',
              `nick_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '昵称',
              `real_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '真实姓名',
              `card` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '身份证',
              `bank_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '开户行',
              `bank_account` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '银行账号',
              `level` int(5) UNSIGNED NOT NULL DEFAULT 0 COMMENT '会员等级\r\n0 普通会员\r\n1 vip',
              `state` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '0' COMMENT '0：正常，1：冻结，-2：删除',
              `openid` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'openid',
              `image` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '会员图像',
              `province` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '省',
              `city` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '市',
              `area` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '区、县',
              `addr` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '详细地址',
              `update_time` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '修改时间',
              `create_time` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '注册时间',
              PRIMARY KEY (`user_id`) USING BTREE,
              INDEX `email`(`email`) USING BTREE,
              INDEX `parent_id`(`parent_id`) USING BTREE,
              INDEX `user_id`(`user_id`) USING BTREE,
              INDEX `integral`(`integral`) USING BTREE,
              INDEX `user_name`(`user_name`) USING BTREE
            ) ENGINE = InnoDB AUTO_INCREMENT = 5778 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
        ");

        //积分变动记录表
        Db::query("DROP TABLE IF EXISTS `qbtcmf_integral_log`");
        Db::query("
            CREATE TABLE `qbtcmf_integral_log`  (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `user_id` int(11) NOT NULL COMMENT '用户id',
              `operation_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '操作类型',
              `num` decimal(10, 2) NULL DEFAULT NULL COMMENT '积分或金额',
              `type` tinyint(1) NULL DEFAULT NULL COMMENT '1 增加\r\n2 扣除',
              `genre` tinyint(1) NULL DEFAULT NULL COMMENT '1 积分\r\n2 可用余额\r\n3 冻结金额',
              `create_time` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '时间',
              `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '变动原因',
              PRIMARY KEY (`id`) USING BTREE
            ) ENGINE = MyISAM AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
        ");

        $data['parent_id'] = 0;
        $data['type'] = 0;
        $data['status'] = 1;
        $data['icon'] = 'user';
        $data['app'] = 'plugin/QbtMemberForMall';
        $data['controller'] = 'AdminIndex';
        $data['action'] = 'index';
        $data['name'] = '会员管理';
        $data['remark'] = '千佰特会员管理';
        $id = Db::table('qbtcmf_admin_menu')->insertGetId($data);
        $data1['parent_id'] = $id;
        $data1['type'] = 0;
        $data1['status'] = 1;
        $data1['app'] = 'plugin/QbtMemberForMall';
        $data1['controller'] = 'AdminIndex';
        $data1['action'] = 'index';
        $data1['name'] = '会员列表';
        $data1['remark'] = '千佰特会员管理';
        Db::table('qbtcmf_admin_menu')->insert($data1);
        $data2['parent_id'] = $id;
        $data2['type'] = 0;
        $data2['status'] = 1;
        $data2['app'] = 'plugin/QbtMemberForMall';
        $data2['controller'] = 'AdminIndex';
        $data2['action'] = 'index1';
        $data2['name'] = '会员账户记录';
        $data2['remark'] = '千佰特会员管理';
        Db::table('qbtcmf_admin_menu')->insert($data2);

        return true;//安装成功返回true，失败false
    }

    // 插件卸载
    public function uninstall()
    {
        Db::query("DROP TABLE `qbtcmf_mall_members`");
        Db::query("DROP TABLE `qbtcmf_integral_log`");
        Db::table('qbtcmf_admin_menu')->where("app = 'plugin/QbtMemberForMall'")->delete();
        return true;//卸载成功返回true，.
    }
}